package com.pcxm.cm.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pcxm.cm.dao.MaxPeriodHome;
import com.pcxm.cm.pojo.MaxPeriod;
import com.pcxm.cm.service.MaxPeriodService;

@Service
public class MaxPeriodServiceImpl implements MaxPeriodService{
	
	@Autowired
	private MaxPeriodHome home;
	
	
	public MaxPeriodHome getHome() {
		return home;
	}

	public void setHome(MaxPeriodHome home) {
		this.home = home;
	}

	/* 
	 *maxPeriod 放入name值，period值，moduleId值
	 */
	@Override
	public void saveMaxPeriod(MaxPeriod maxPeriod) {
		// TODO Auto-generated method stub
		MaxPeriodHome maxPeriodHome = (MaxPeriodHome)home;
		MaxPeriod maxPeriod_query = maxPeriodHome.getObj(maxPeriod.getName(),maxPeriod.getModuleId());
		//去数据库根据moduleId和name值查询记录，查不到返回null
		if(maxPeriod_query != null){
//			maxPeriod.setId(maxPeriod_query.getId());
//			maxPeriodHome.merge(maxPeriod);
			maxPeriod_query.setPeriod(maxPeriod.getPeriod());
			maxPeriod_query.setCatchTime(maxPeriod.getCatchTime());
			maxPeriodHome.update(maxPeriod_query);
		}else{
			maxPeriod.setId(maxPeriodHome.getId());
			maxPeriodHome.save(maxPeriod);
		}
	}
	
	/**
	 * @param name
	 * @param moduleId
	 * @return 最大period
	 */
	@Override
	public Long getMaxPeriod(String name,String moduleId) {
		MaxPeriodHome maxPeriodHome = (MaxPeriodHome)home;
		return maxPeriodHome.getMaxPeriod(name, moduleId);
	}

}
